class Solution(object):
    def largestSubmatrix(self, matrix):
        m = len(matrix)
        n = len(matrix[0])
        pre = [[0 for _ in range(n)] for _ in range(m)]
        for j in range(n):
            for i in range(m):
                if i == 0:
                    pre[i][j] = matrix[i][j]
                else:
                    if matrix[i][j]:
                        pre[i][j] = pre[i - 1][j] + 1
                    else:
                        pre[i][j] = 0
        max_len = 0
        for i in range(m):
            t = pre[i]
            t.sort(reverse=True)
            now_max = float('inf')
            for j in range(n):
                if pre[i][j] > 0:
                    now_max = min(now_max, pre[i][j])
                    max_len = max(max_len, now_max * (j + 1))
                else:
                    break
        return max_len
